package pl.edu.pk.mech.kkbus.service;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.Query;

import org.springframework.stereotype.Service;

import pl.edu.pk.mech.kkbus.component.MyEntityManagerFactory;
import pl.edu.pk.mech.kkbus.entities.Defect;

@Service
public class DefectService {
    
    EntityManagerFactory emf = MyEntityManagerFactory.get();
    
    public List<pl.edu.pk.mech.kkbus.dto.Defect> getAll() {
        List<pl.edu.pk.mech.kkbus.dto.Defect> result = 
                new LinkedList<pl.edu.pk.mech.kkbus.dto.Defect>();
        EntityManager em = null;        
        try {
            em = emf.createEntityManager();
            Query query = em.createQuery("SELECT d FROM Defect d");
            List<Defect> results = (List<Defect>) query.getResultList();
            Iterator<Defect> iterator = results.iterator();
            while (iterator.hasNext()) {  
                Defect d = iterator.next();
                
                // TODO Select buss
                
                result.add(new pl.edu.pk.mech.kkbus.dto.Defect(d.getKey().getId(),
                        d.getDescription(), d.getCost(), d.getDate(), d.getBusId()));
            }
            return result;         
        } catch(NoResultException e) {
            return null;
        } finally {
            if (em != null)
                em.close();
        }
    } 
    
    public boolean create(pl.edu.pk.mech.kkbus.dto.Defect d) {
        EntityManager em = null;        
        try {
            em = emf.createEntityManager();
            Defect defect = new Defect(d.getDescription(), d.getCost(),
                    d.getDate(), d.getBusId());
            em.persist(defect);
            
            return true;
        } catch(Exception e) {
            return false;
        } finally {
            if (em != null)
                em.close();
        }
    }
    
    public boolean update(long id, pl.edu.pk.mech.kkbus.dto.Defect d) {
        EntityManager em = null;        
        try {
            em = emf.createEntityManager();
            Defect defect = em.find(Defect.class, new Long(id));
            defect.setBusId(d.getBusId());
            defect.setCost(d.getCost());
            defect.setDate(d.getDate());
            defect.setDescription(d.getDescription());
            
            em.persist(defect);
            
            return true;
        } catch(Exception e) {
            return false;
        } finally {
            if (em != null)
                em.close();
        }
    }
    
    public boolean delete(long id) {
        EntityManager em = null;        
        try {
            em = emf.createEntityManager();
            Defect defect = em.find(Defect.class, new Long(id));          
            em.remove(defect);
            
            return true;
        } catch(Exception e) {
            return false;
        } finally {
            if (em != null)
                em.close();
        }
    }    
}
